﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NPOI.HSSF.UserModel;
using System.IO;
using NPOI.POIFS.FileSystem;

namespace BOM.PriceBill
{
    public partial class DerivesExcel : Form
    {
        public DerivesExcel()
        {
            InitializeComponent();
        }

        //确定导出按钮
        private void btnderives_Click(object sender, EventArgs e)
        {
            try
            {
                String filepath = "D:/vsws/pricebill/Resources/ExpTemple.xls";
                FileStream file = new FileStream(filepath, FileMode.Open, FileAccess.Read);
                HSSFWorkbook book = new HSSFWorkbook(file);
                HSSFSheet sheet = book.GetSheet("Sheet1");
                string[] LineArray = PriceBillForm.ExcelString.Split('#');
                HSSFRow ExcelRow = sheet.CreateRow(0);
                HSSFCell ExcelCell = ExcelRow.CreateCell(0, HSSFCell.CELL_TYPE_NUMERIC);
                ExcelCell.SetCellValue("" + LineArray[0] + "");
                for (int i = 1; i < LineArray.Length; i++)
                {
                    ExcelRow = sheet.CreateRow(i);
                    int k = 1; //记下写入的是几的一列
                    string[] ListArray = LineArray[i].Split('|');
                    if (i < 3)
                    {
                        for (int j = 0; j < ListArray.Length; j++)
                        {
                            ExcelCell = ExcelRow.CreateCell(k, HSSFCell.CELL_TYPE_NUMERIC);
                            ExcelCell.SetCellValue(ListArray[j]);
                            //MessageBox.Show(ListArray[j]);
                            k += 2;
                        }
                    }
                    if (i > 3)
                    { 
                        k=0;
                        string[] ListArrayl = LineArray[i-1].Split('|');
                        for (int j = 0;  j < ListArrayl.Length;j++)
                        {
                            ExcelCell = ExcelRow.CreateCell(k, HSSFCell.CELL_TYPE_NUMERIC);
                            ExcelCell.SetCellValue(ListArrayl[j]);
                            k ++;
                        }
                    }

                }
                ExcelRow = sheet.CreateRow(14);
                string[] ListArrayh = LineArray[LineArray.Length-1].Split('|');
                int l = 1;
                for (int j = 0; j < ListArrayh.Length; j++)
                {
                    ExcelCell = ExcelRow.CreateCell(l, HSSFCell.CELL_TYPE_NUMERIC);
                    ExcelCell.SetCellValue(ListArrayh[j]);
                    l += 2;
                }

                using (FileStream stream = new FileStream("" + txtPath.Text + @"\" + txtName.Text + ".xls", FileMode.OpenOrCreate, FileAccess.Write))
                {
                    book.Write(stream);
                }
                MessageBox.Show("导出成功");
            }
            catch(Exception e5)
            {
                MessageBox.Show(e5.Message);
            }
        }
        //窗体的加载事件
        private void DerivesExcel_Load(object sender, EventArgs e)
        {
            string str = PriceBillForm.ExcelString;

            string[] sArray = str.Split('#');
            if (sArray[0].Trim() != "计价表")
            {
                txtName.Text = sArray[0];
            }
            else
            {
                MessageBox.Show("您没选择任何计价表", "提示");
            }
        }
        //选择路径按钮
        private void btnSelect_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowDialog();
            txtPath.Text = fbd.SelectedPath;
        }
        //取消按钮
        private void btnend_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
